package cn.client.ui.view.main.chat.friend

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.PopupProperties
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.ui.components.ThisUserInfoCard

/**
 * 好友信息页面
 */
@Composable
fun FriendInfoPage(
    userId: Int,
    friendId: Int,
    navController: NavController,
    viewModel: FriendViewModel = hiltViewModel()
) {
    // 观察好友信息状态
    val friendInfoResState = viewModel.friendInfoRes.collectAsState(initial = null)
    val friendInfoRes = friendInfoResState.value
    // 初始加载发送意图
    viewModel.dispatch(FriendViewModel.PageIntent.FriendInfo(fid = friendId))

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        /* 顶部导航栏 */
        FriendInfoTopBar(
            onClickNavigation = { navController.navigateUp() }
        )
        // 好友信息视图
        friendInfoRes?.let { it ->
            ThisUserInfoCard(
                avatarURL = it.avatar,
                backGroundURL = it.background,
                userName = it.uname,
                email = it.email,
                birthday = it.birthday,
                area = it.area
            )
        }
        // 内容区域
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(5.dp)
        ) {
            Button(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(5.dp),
                onClick = {
                    /* 携带uid和fid进入聊天页面 */

                }) {
                Text(text = "进入聊天")
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FriendInfoTopBar(
    onClickNavigation: () -> Unit
) {
    data class DropdownItem(val title: String, val id: Int)

    val dropdownList = listOf(
        DropdownItem("设置", 1),
    )
    var expanded by remember { mutableStateOf(false) }

    TopAppBar(
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer,
            titleContentColor = MaterialTheme.colorScheme.primary
        ),
        title = { Text("好友信息") },
        /* 左边按钮 */
        navigationIcon = {
            IconButton(onClick = {
                onClickNavigation()
            }
            ) {
                Icon(Icons.AutoMirrored.Filled.ArrowBack, null)

            }
            Spacer(modifier = Modifier.size(24.dp))
        },
        /* 右边按钮（可以是多个） */
        actions = {
            IconButton(onClick = { expanded = !expanded }
            ) {
                Icon(Icons.Filled.MoreVert, null)
                /* 下拉框 */
                DropdownMenu(
                    expanded = expanded,
                    onDismissRequest = { expanded = !expanded }, // 当下拉菜单展开/关闭时更新状态
                    modifier = Modifier.wrapContentSize(),
                    properties = PopupProperties(
                        focusable = true,
                        dismissOnBackPress = false,
                        dismissOnClickOutside = true
                    )
                ) {
                    dropdownList.forEach { dropItem ->
                        DropdownMenuItem(
                            { Text(text = dropItem.title) },
                            onClick = {
                                expanded = false
                                when (dropItem.id) {
                                    1 -> { /* 发送意图 */

                                    }

                                    else -> {
                                        Unit
                                    }
                                }

                            }
                        )
                    }
                }
            }
        }
    )
}
